﻿using System;
using System.Data;
using System.Data.SqlClient;

namespace Module.QuanTriHeThong.DanhMuc
{
    public class DanhMucTaiKhoan : DBInteractionBase
    {
        public string MaNhanVien { get; set; }
        public string TaiKhoan { get; set; }
        public string MatKhau { get; set; }
        public string HoTen { get; set; }
        public DateTime NgaySinh { get; set; }
        public string SoCMTND { get; set; }
        public string DiaChi { get; set; }
        public string SoDienThoai { get; set; }
        public string MaChucVu { get; set; }
        public string MaCuaHang { get; set; }
        public bool IsSuperAdmin { get; set; }
        public DateTime NgayTao { get; set; }
        public bool TrangThaiDangNhap { get; set; }
        public DateTime LanCuoiDangNhap { get; set; }
        public bool IsActive { get; set; }

        public bool Insert(DanhMucTaiKhoan obj, string taikhoanlog)
        {
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucTaiKhoan_Insert", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", ConvertNvarcharToVarchar(obj.TaiKhoan).ToLower()));
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoanLog", taikhoanlog));
                    dbcmd.Parameters.Add(new SqlParameter("@HoTen", obj.HoTen));
                    dbcmd.Parameters.Add(new SqlParameter("@NgaySinh", obj.NgaySinh));
                    dbcmd.Parameters.Add(new SqlParameter("@SoCMTND", obj.SoCMTND));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoDienThoai", obj.SoDienThoai));
                    dbcmd.Parameters.Add(new SqlParameter("@MaChucVu", obj.MaChucVu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaCuaHang", obj.MaCuaHang));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool Update(DanhMucTaiKhoan obj)
        {
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucTaiKhoan_Update", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaNhanVien", obj.MaNhanVien));
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", obj.TaiKhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@HoTen", obj.HoTen));
                    dbcmd.Parameters.Add(new SqlParameter("@NgaySinh", obj.NgaySinh));
                    dbcmd.Parameters.Add(new SqlParameter("@SoCMTND", obj.SoCMTND));
                    dbcmd.Parameters.Add(new SqlParameter("@DiaChi", obj.DiaChi));
                    dbcmd.Parameters.Add(new SqlParameter("@SoDienThoai", obj.SoDienThoai));
                    dbcmd.Parameters.Add(new SqlParameter("@MaChucVu", obj.MaChucVu));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public bool KhoaTaiKhoan(string taikhoan, string manhanvien, bool isactive)
        {
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucTaiKhoan_KhoaTaiKhoan", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", taikhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@MaNhanVien", manhanvien));
                    dbcmd.Parameters.Add(new SqlParameter("@IsActive", isactive));
                    dbcmd.ExecuteNonQuery();
                }
                return true;
            }
            catch (Exception)
            {
                return false;
            }
        }

        public DataTable SelectByTaiKhoan(string taikhoan)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucTaiKhoan_SelectByTaiKhoan", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan", taikhoan));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DanhMucTaiKhoan_SelectByTaiKhoan: " + ex.Message);
            }
        }

        public DataTable SelectByMaNhanVien(string manhanvien)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucTaiKhoan_SelectByMaNhanVien", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@MaNhanVien", manhanvien));
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DanhMucTaiKhoan_SelectByMaNhanVien: " + ex.Message);
            }
        }

        public DataTable SelectAll(DanhMucTaiKhoan obj, int pageindex, int pagesize, out int recordcount)
        {
            var dt = new DataSet();
            try
            {
                using (var dbcmd = new SqlCommand("DanhMucTaiKhoan_SelectAll", GetConnection()))
                {
                    dbcmd.CommandType = CommandType.StoredProcedure;
                    dbcmd.Parameters.Add(new SqlParameter("@TaiKhoan",string.IsNullOrEmpty(obj.TaiKhoan) ? "" : obj.TaiKhoan));
                    dbcmd.Parameters.Add(new SqlParameter("@HoTen",string.IsNullOrEmpty(obj.HoTen) ? "" : obj.HoTen));
                    dbcmd.Parameters.Add(new SqlParameter("@SoCMTND", string.IsNullOrEmpty(obj.SoCMTND) ? "" : obj.SoCMTND));
                    dbcmd.Parameters.Add(new SqlParameter("@MaChucVu", string.IsNullOrEmpty(obj.MaChucVu) ? "" : obj.MaChucVu));
                    dbcmd.Parameters.Add(new SqlParameter("@MaCuaHang", string.IsNullOrEmpty(obj.MaCuaHang) ? "" : obj.MaCuaHang));
                    dbcmd.Parameters.Add(new SqlParameter("@PageIndex", pageindex));
                    dbcmd.Parameters.Add(new SqlParameter("@PageSize", pagesize));
                    dbcmd.Parameters.Add(new SqlParameter("@RecordCount", SqlDbType.Int, 50)
                    {
                        Direction = ParameterDirection.Output
                    });
                    var dtAdapter = new SqlDataAdapter(dbcmd);
                    dtAdapter.Fill(dt);
                    recordcount = Convert.ToInt32(dbcmd.Parameters["@RecordCount"].Value);
                    return dt.Tables[0];
                }
            }
            catch (Exception ex)
            {
                throw new Exception("DanhMucTaiKhoan_SelectAll: " + ex.Message);
            }
        }
    }
}
